package com.jiangchh.product_service.service.impl;

import com.jiangchh.product_service.domain.Product;
import com.jiangchh.product_service.service.ProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ProductServiceImpl implements ProductService {

    private static final Map<Integer,Product> daoMap = new HashMap<>();
    private final Logger logger = LoggerFactory.getLogger(getClass());

    static {

        Product product1 = new Product(1,"iphone1",1111,1111);
        Product product2 = new Product(2,"iphone2",2222,2222);
        Product product3 = new Product(3,"iphone3",3333,3333);
        Product product4 = new Product(4,"iphone4",4444,4444);
        Product product5 = new Product(5,"iphone5",5555,5555);
        Product product6 = new Product(6,"iphone6",6666,6666);
        Product product7 = new Product(7,"iphone7",7777,7777);

        daoMap.put(product1.getId(),product1);
        daoMap.put(product2.getId(),product2);
        daoMap.put(product3.getId(),product3);
        daoMap.put(product4.getId(),product4);
        daoMap.put(product5.getId(),product5);
        daoMap.put(product6.getId(),product6);
        daoMap.put(product7.getId(),product7);
    }

    @Override
    public List<Product> listProduct() {

        Collection<Product> collection = daoMap.values();
        List<Product> list = new ArrayList<>(collection);
        return list;
    }

    @Override
    public Product findById(int id) {
        logger.info("service findById");
        return daoMap.get(id);
    }
}
